﻿
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace File_Endings
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>	
	public partial class MainForm : Form
	{
		private string path, ending;
		private List<string> endings, file;
		private List<int> numberof;
		private bool casesensitive = false;
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			file = new List<string>();
		}
		
		void ButtonPathClick(object sender, EventArgs e)
		{
			FolderBrowserDialog f = new FolderBrowserDialog();
			if (f.ShowDialog() == DialogResult.OK)
			{
				path = f.SelectedPath;
				this.textBoxPath.Text = path;
				this.buttonStart.Enabled = true;
			}
		}
		
		void ListBoxEndingsSelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				ending = this.listBoxEndings.SelectedItem.ToString();
				int pos = ending.IndexOf(' ');
				ending = ending.Remove(pos);
				this.textBoxEnding.Text = ending;
			}
			catch(Exception ex){}
		}
		
		void ButtonStartClick(object sender, EventArgs e)
		{
			start();
		}
		
		void start()
		{
			this.listBoxEndings.Items.Clear();
			this.listBoxFiles.Items.Clear();
			endings = new List<string>();
			numberof = new List<int>();
			searchFolder(path);
			for(int i = 0; i < endings.Count; i++)
			{
				this.listBoxEndings.Items.Add(endings[i] + " (" + numberof[i] + ")");
			}
			this.buttonShow.Enabled = true;
		}
		
		void searchFolder(string path)
		{
			string[] subFolders = Directory.GetDirectories(path);
			string[] files = Directory.GetFiles(path);
			int pos;
			string e;
			for (int i = 0; i < files.Length; i++)
			{
				pos = files[i].LastIndexOf('.');
				try
				{
					e = files[i].Remove(0, pos);
				}
				catch (ArgumentOutOfRangeException ae)
				{
					e = "No Ending";
				}
				if(!casesensitive)
					e = e.ToLower();
				if(!endings.Contains(e))
				{
					endings.Add(e);
					numberof.Add(1);
				}
				else
				{
					pos = endings.IndexOf(e);
					numberof[pos]++;
				}
				this.file.Add(files[i]);
			}
			if (subFolders.Length != 0)
			{
				for(int i = 0; i < subFolders.Length; i++)
				{
					searchFolder(subFolders[i]);
				}
			}
		}
		
		void CasesensitiveToolStripMenuItemClick(object sender, EventArgs e)
		{
			this.casesensitiveToolStripMenuItem.Checked = !this.casesensitiveToolStripMenuItem.Checked;
			casesensitive = this.casesensitiveToolStripMenuItem.Checked;
		}
		
		void ButtonShowClick(object sender, EventArgs e)
		{
			endings = new List<string>();
			this.listBoxFiles.Items.Clear();
			searchForEnding(path, ending);
			for(int i = 0; i < endings.Count; i++)
			{
				this.listBoxFiles.Items.Add(endings[i]);
			}
			this.deleteChoosenFilesToolStripMenuItem.Enabled = true;
		}
		
		void searchForEnding(string path, string ending)
		{
			int pos;
			string e;
			for (int i = 0; i < file.Count; i++)
			{
				pos = file[i].LastIndexOf('.');
				try
				{
					e = file[i].Remove(0, pos);
				}
				catch (ArgumentOutOfRangeException ae)
				{
					e = "No Ending";
				}
				if(!casesensitive)
					e = e.ToLower();
				if(e == ending)
					endings.Add(file[i]);
			}
		}
		
		void DeleteChoosenFilesToolStripMenuItemClick(object sender, EventArgs e)
		{
			Delete d = new Delete(ending, path);	
			if (d.ShowDialog() == DialogResult.OK)
			{	
				List<object> tmp = new List<object>();
				bool f;
				for(int i = 0; i< file.Count; i++) //save all items except the files which will be deleted
				{
					f = false;
					for(int j = 0; j < this.listBoxFiles.Items.Count; j++)
					{
						if(file[i] == this.listBoxFiles.Items[j].ToString())
							f = true;
					}		
					if(!f)
						tmp.Add(file[i]);
				}
				file = new List<string>();
				for(int i = 0; i < tmp.Count; i++) //save the items form tmp into file
				{
					file.Add(tmp[i].ToString());
				}
				for(int i = 0; i < this.listBoxFiles.Items.Count; i++) //delete the items
				{
					File.Delete(this.listBoxFiles.Items[i].ToString());
				}
				tmp = new List<object>();
				for(int i = 0; i< this.listBoxEndings.Items.Count; i++) //save all endings
				{
					tmp.Add(this.listBoxEndings.Items[i]);
				}
				this.listBoxEndings.Items.Clear();
				for(int i = 0; i< tmp.Count; i++) //save all endings in the list box except the deleted
				{
					if(!tmp[i].ToString().StartsWith(ending))
					{
						this.listBoxEndings.Items.Add(tmp[i].ToString());
					}
				}
				this.listBoxFiles.Items.Clear();
				this.deleteChoosenFilesToolStripMenuItem.Enabled = false;
			}
		}
		
		void CloseToolStripMenuItemClick(object sender, EventArgs e)
		{
			this.Close();
		}
	}
}
